require 'yaml'
require 'active_record'

module WPSpider
  module Common

    #
    # DBConnector class.
    # Connects DB using ActiveRecord.
    #
    class DBConnector
      # Hash map of DB configuration.
      attr_accessor :conf

      #
      # Constructor.
      #
      def initialize()
        @conf = Hash.new()
      end

      #
      # Loads DB configuration from YAML file.
      #
      def load_conffile(file)
        @conf = YAML::load(File::open(file, 'r').read())
        @conf.each() { |key, val|
          @conf[key.to_sym] = val if(!key.instance_of?(Symbol))
        }
      end

      #
      # Connects DB.
      #
      def connect()
        ActiveRecord::Base.establish_connection(@conf)
      end
    end
  end
end
